<?php

namespace app\admin\model;

use think\Model;


class Centralcontact extends Model
{





    // 表名
    protected $name = 'centralcontact';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'create_time';
    protected $updateTime = 'update_time';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'access_time_text',
        'tmp_access_time_text',
        'sign_time_text',
        'tmp_sign_time_text',
        'receive_time_text',
        'tmp_receive_time_text',
        'create_time_text',
        'update_time_text',
        'tmp_investment_total',
        'stop_time_text',
        'tmp_stop_time_text',
        'framework_agreement_time_text',
        'tmp_framework_agreement_time_text',
        'invesment_agreement_time_text',
        'tmp_invesment_agreement_time_text',
        'register_time',
        'tmp_register_time',
        'start_construction_time_text',
        'tmp_start_construction_time_text',
        'operation_time_text',
        'tmp_operation_time_text',
        'current_state_time_text'
    ];



    protected static function init()
    {
        Centralcontact::event('before_insert',function ($data){
            $data -> updateRelaByAdmin($data,session('admin.id') == 1);
            if(session('admin.id') != 1){
                // $data['is_remind'] = 1;
            }else{
                // $data['status'] = 300;
                // $data['is_remind'] = 0;
            }
            $data['is_remind'] = 1;
        });

        Centralcontact::event('before_update',function ($data){
            $data -> updateRelaByAdmin($data,session('admin.id') == 1);
            // if(session('admin.id') != 1){
            // }else{
            //     $data['is_remind'] = 0;
            // }
        });
    }

    protected function updateRelaByAdmin($data, $isAdmin){
        $depKey = $isAdmin ? 'chargedep_ids' : 'tmp_chargedep_ids';
        $depNamesKey = $isAdmin ? 'chargedep_names' : 'tmp_chargedep_names';
        if(isset($data[$depKey]) && $data[$depKey]){
            if($data[$depKey] == '-100'){
                $data[$depNamesKey] = '待定';
            }else{
                $data[$depNamesKey] = (new \app\admin\model\Chargedep())->whereIn('id', $data[$depKey])->value('GROUP_CONCAT(name)');
            }
        }

        $streetKey = $isAdmin ? 'settle_street_ids' : 'tmp_settle_street_ids';
        $streetNamesKey = $isAdmin ? 'settle_street_names' : 'tmp_settle_street_names';
        if(isset($data[$streetKey]) && $data[$streetKey]){
            if($data[$streetKey] == '-100'){
                $data[$streetNamesKey] = '待定';
            }else {
                $data[$streetNamesKey] = (new \app\admin\model\Chargedep())->whereIn('id', $data[$streetKey])->value('GROUP_CONCAT(name)');
            }
        }

        $leaderKey = $isAdmin ? 'leader_ids' : 'tmp_leader_ids';
        $leaderNamesKey = $isAdmin ? 'leader_names' : 'tmp_leader_names';
        if(isset($data[$leaderKey]) && $data[$leaderKey]) {
            if($data[$leaderKey] == '-100'){
                $data[$leaderNamesKey] = '无';
            }else{
                $data[$leaderNamesKey] = (new \app\admin\model\Admin())->whereIn('id', $data[$leaderKey])->value('GROUP_CONCAT(nickname)');
            }
        }

        $officeKey = $isAdmin ? 'centraloffice_id' : 'tmp_centraloffice_id';
        $officeNamesKey = $isAdmin ? 'centraloffice_name' : 'tmp_centraloffice_name';
        if(isset($data[$officeKey])){
            $data[$officeNamesKey] = (new Centraloffice())->whereIn('id', $data[$officeKey])->value('GROUP_CONCAT(name)');
        }
    }


    public function getAccessTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['access_time']) ? $data['access_time'] : '');
        return is_numeric($value) && $value > 0 ? date("Y-m-d", $value) : ($value <= 0 ? '' : $value);
    }


    public function getTmpAccessTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['tmp_access_time']) ? $data['tmp_access_time'] : '');
        return is_numeric($value) ? date("Y-m-d", $value) : $value;
    }


    public function getSignTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['sign_time']) ? $data['sign_time'] : '');
        return is_numeric($value) && $value > 0 ? date("Y-m-d", $value) : '';
    }

    public function getStopTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['stop_time']) ? $data['stop_time'] : '');
        return is_numeric($value) && $value > 0 ? date("Y-m-d", $value) : '';
    }

    public function getTmpStopTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['tmp_stop_time']) ? $data['tmp_stop_time'] : '');
        return is_numeric($value) && $value > 0 ? date("Y-m-d", $value) : '';
    }


    public function getFrameworkAgreementTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['framework_agreement_time']) ? $data['framework_agreement_time'] : '');
        return is_numeric($value) && $value > 0 ? date("Y-m-d", $value) : '';
    }

    public function getTmpFrameworkAgreementTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['tmp_framework_agreement_time']) ? $data['tmp_framework_agreement_time'] : '');
        return is_numeric($value) && $value > 0 ? date("Y-m-d", $value) : '';
    }

    public function getInvesmentAgreementTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['invesment_agreement_time']) ? $data['invesment_agreement_time'] : '');
        return is_numeric($value) && $value > 0 ? date("Y-m-d", $value) : '';
    }

    public function getTmpInvesmentAgreementTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['tmp_invesment_agreement_time']) ? $data['tmp_invesment_agreement_time'] : '');
        return is_numeric($value) && $value > 0 ? date("Y-m-d", $value) : '';
    }


    public function getStartConstructionTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['start_construction_time']) ? $data['start_construction_time'] : '');
        return is_numeric($value) && $value > 0 ? date("Y-m-d", $value) : '';
    }

    public function getTmpStartConstructionTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['tmp_start_construction_time']) ? $data['tmp_start_construction_time'] : '');
        return is_numeric($value) && $value > 0 ? date("Y-m-d", $value) : '';
    }

    public function getOperationTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['operation_time']) ? $data['operation_time'] : '');
        return is_numeric($value) && $value > 0 ? date("Y-m-d", $value) : '';
    }

    public function getTmpOperationTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['tmp_operation_time']) ? $data['tmp_operation_time'] : '');
        return is_numeric($value) && $value > 0 ? date("Y-m-d", $value) : '';
    }

    public function getTmpSignTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['tmp_sign_time']) ? $data['tmp_sign_time'] : '');
        return is_numeric($value) ? date("Y-m-d", $value) : $value;
    }


    public function getRegisterTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['register_time']) ? $data['register_time'] : '');
        return is_numeric($value) ? date("Y-m-d", $value) : $value;
    }


    public function getTmpRegisterTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['tmp_register_time']) ? $data['tmp_register_time'] : '');
        return is_numeric($value) ? date("Y-m-d", $value) : $value;
    }


    public function getReceiveTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['receive_time']) ? $data['receive_time'] : '');
        return is_numeric($value) ? date("Y-m-d", $value) : $value;
    }


    public function getTmpReceiveTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['tmp_receive_time']) ? $data['tmp_receive_time'] : '');
        return is_numeric($value) ? date("Y-m-d", $value) : $value;
    }


    public function getCreateTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['create_time']) ? $data['create_time'] : '');
        return is_numeric($value) ? date("Y-m-d", $value) : $value;
    }


    public function getUpdateTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['update_time']) ? $data['update_time'] : '');
        return is_numeric($value) ? date("Y-m-d", $value) : $value;
    }

    protected function setAccessTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setTmpAccessTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setStopTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setTmpStopTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }


    protected function setFrameworkAgreementTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setTmpFrameworkAgreementTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setInvesmentAgreementTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setTmpInvesmentAgreementTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setStartConstructionTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setTmpStartConstructionTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setOperationTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setTmpOperationTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setSignTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setTmpSignTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setRegisterTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setTmpRegisterTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setReceiveTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setTmpReceiveTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setCreateTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setUpdateTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setChargedepIdsAttr($value){
        return is_array($value) ? implode(',',$value) : $value;
    }

    protected function setSettleStreetIdsAttr($value){
        if(is_array($value) && count($value) > 1){
            return is_array($value) ? implode(',',array_diff($value,[-100])) : $value;
        }else{
            return is_array($value) ? implode('',$value) : $value;
        }
    }

    protected function setLeaderIdsAttr($value){
        return is_array($value) ? implode(',',$value) : $value;
    }

    protected function setTmpChargedepIdsAttr($value){
        return is_array($value) ? implode(',',$value) : $value;
    }

    protected function setIsNewAttr($value){
        return trim($value) == '是' ? 1 : 0;
    }

    protected function setTmpSettleStreetIdsAttr($value){
        if(is_array($value) && count($value) > 1){
            return is_array($value) ? implode(',',array_diff($value,[-100])) : $value;
        }else{
            return is_array($value) ? implode('',$value) : $value;
        }
    }

    protected function setTmpLeaderIdsAttr($value){
        return is_array($value) ? implode(',',$value) : $value;
    }

    protected function setCentralofficeIdAttr($value){
        return is_array($value) ? implode(',',$value) : $value;
    }

    protected function setTmpCentralofficeIdAttr($value){
        return is_array($value) ? implode(',',$value) : $value;
    }

    public function setTmpInvestmentTotalAttr($v){
        return $v != '待定' ? $v : -100;
    }

    public function getTmpInvestmentTotalAttr($v,$d){
        return $v != -100 ? $v : '待定';
    }

    public function setInvestmentTotalAttr($v){
        return $v != '待定' ? $v : -100;
    }

    public function getInvestmentTotalAttr($v,$d){
        return $v != -100 ? $v : '待定';
    }

    public function progress(){
        return $this -> hasMany('centralcontact_progress','name','centralcontact_name') -> order('progress_time','desc');
    }

    public function getCurrentStateTimeTextAttr($v,$d){
        switch ($d['state']){
            case '终止':
                return $this -> stop_time_text;
            case '暂停':
            case '在谈':
                return '-';
            case '框架':
                return $this -> framework_agreement_time_text;
            case '正式':
                return $this -> invesment_agreement_time_text;
            case '注册':
                return $this -> register_time_text;
            case '开工':
                return $this -> start_construction_time_text;
            case '运营':
                return $this -> operation_time_text;
            default:
                return '-';
        }
    }
}
